package com.base.cn.platform.os.common.service;

import com.base.cn.platform.os.common.mybatis.BaseEntity;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.service.manage.sysUser.SysUserService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公共设置创建用户数据Service
 *
 * @author s.li
 * @create 2018-04-19-18:37
 */
@Service
public class CommonSetCreateUserDataService<E extends BaseEntity> {

    /**
     * 设置创建人员数据
     * @param e 数据对象
     * @param sysUserData 是否设置创建人员数据，true设置，false不设置
     * @return Course
     */
    public E setSysUserData(SysUserService sysUserService, E e, boolean sysUserData){
        if(ObjectUtils.isNotEmpty(e) && sysUserData){
            List<E> list = new ArrayList<>();
            list.add(e);
            list = this.setSysUserData(sysUserService,list,sysUserData);
            e = list.get(0);
        }
        return e;
    }
    /**
     * 设置创建人员数据
     * @param list 数据列表
     * @param sysUserData 是否设置创建人员数据，true设置，false不设置
     * @return
     */
    public List<E> setSysUserData(SysUserService sysUserService,List<E> list, boolean sysUserData){
        if(ObjectUtils.isNotEmpty(list) && sysUserData){
            String userIds = list.stream().filter(e-> DataUtil.idIsNotNull(e.getCreateUserId())).map(e->e.getCreateUserId().toString()).collect(Collectors.joining(","));
            Map<BigDecimal,Map<String,Object>> userMaps = sysUserService.findSysUserMapByUserIds(userIds,false,false,false);
            if(ObjectUtils.isNotEmpty(userMaps)){
                list.forEach(e->{
                    Map<String,Object> userMap = userMaps.get(e.getCreateUserId());
                    e.setCreateUser(userMap);
                });
            }
        }
        return list;
    }
}
